//---------------------------------------------------------------------
//  This file is part of the Microsoft .NET Framework SDK Code Samples.
// 
//  Copyright (C) Microsoft Corporation.  All rights reserved.
// 
//This source code is intended only as a supplement to Microsoft
//Development Tools and/or on-line documentation.  See these other
//materials for detailed information regarding Microsoft code samples.
// 
//THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//PARTICULAR PURPOSE.
//---------------------------------------------------------------------

using System;
using System.Collections;
using System.Text;
using System.Resources;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;

namespace TaskVision.Components
{
    class DataGridViewTasks : System.Windows.Forms.DataGridView 
    {

        private Hashtable m_HtImages = new Hashtable();
        private const string c_PriorityImagesPath = @"Images\";
        private ResourceManager m_ResourceManager = new ResourceManager("TaskVision.Localize", System.Reflection.Assembly.GetExecutingAssembly());


        public DataGridViewTasks()
        {
            ApplyStyle();
        }

        public void ApplyStyle()
        {
            // Must be called before populating data 

            // Alternate the colors of rows
            RowsDefaultCellStyle.BackColor = System.Drawing.Color.White;
            AlternatingRowsDefaultCellStyle.BackColor = System.Drawing.Color.Wheat;

            this.RowHeadersVisible = false;
            //Columns["PriorityText"].RaiseCellValueChanged += new System.EventHandler(this.setImage);
            
        }

        public void formatImage(DataGridViewCellFormattingEventArgs imageCell)
        {
            //object bVal = GetColumnValueAtRow(source, rowNum);
            //object bVal = cell.Value;
            //String priority = cell.Value.ToString();
            //HebMessageBox.Show(Application.StartupPath + @"\" +c_PriorityImagesPath + priority + ".gif");
            Image imageToDraw;
            String priority = this[imageCell.ColumnIndex, imageCell.RowIndex].Value.ToString();
          
            // we're caching the image in a hashtable

            if (m_HtImages.ContainsKey(priority))
                imageToDraw = (Image)m_HtImages[priority];
            else
            {
                // get the image from disk and cache it
                try
                {
                    //imageToDraw = Image.FromFile(c_PriorityImagesPath + (string)bVal + ".gif");
                    //m_HtImages.Add(bVal, imageToDraw);
                    imageToDraw = Image.FromFile(c_PriorityImagesPath + priority + ".gif");
                    m_HtImages.Add(priority, imageToDraw);
                }
                catch
                {
                    HebMessageBox.Show(m_ResourceManager.GetString("The_image_file") + " " +
                            Application.StartupPath + @"\" + c_PriorityImagesPath +
                            priority +
                            m_ResourceManager.GetString("gif_was_not_found"),
                            m_ResourceManager.GetString("File_Not_Found"));
                    LogError.Write(m_ResourceManager.GetString("The_image_file") + " " +
                            Application.StartupPath + @"\" + c_PriorityImagesPath +
                            priority + m_ResourceManager.GetString("gif_was_not_found"));
                    return;  // Necessary in C# -- otherwise it won't compile
                    // because imageToDraw is used below and might not be assigned.
                }
            }
            imageCell.Value = imageToDraw;
            // For app performance, set FormattingApplied so that formatting happens only once
            imageCell.FormattingApplied = true;
        }

        public void formatDate(DataGridViewCellFormattingEventArgs cell)
        {
            if (Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName == "de")
                cell.Value = String.Format("{0:d}", Convert.ToDateTime(cell.Value).ToString("dd/MM/yy"));
            else
                cell.Value = String.Format("{0:d}", Convert.ToDateTime(cell.Value));
                
            if (this.Columns[cell.ColumnIndex].Name == "DateDue") 
            {
                cell.CellStyle.ForeColor = Color.Red;
            }
            cell.FormattingApplied = true;
        }


    }
}
